<template>
  <div class="login-page">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="login-title">校园生活分享平台</span>
      </div>
      <div class="login-form">
        <el-form :model="form" :rules="loginRules" ref="loginForm" v-loading="loading">
          <el-form-item prop="studentId">
            <el-input type="text" v-model="form.studentId" auto-complete="off" placeholder="请输入学号">
              <template slot="prepend"><i style="font-size:20px" class="el-icon-document"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="username">
            <el-input type="text" v-model="form.username" auto-complete="off" placeholder="请输入用户名">
              <template slot="prepend"><i style="font-size:20px" class="el-icon-user"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="text" v-model="form.password" auto-complete="off" placeholder="请输入密码" show-password>
              <template slot="prepend"><i style="font-size:20px" class="el-icon-key"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width:100%;" type="primary" @click="Login('loginForm')" :loading="loading">登录</el-button>
          </el-form-item>
          <el-form-item>
            <span  style="margin-left: 180px;"><a style="cursor: pointer;color: #409EFF;" @click="register()">是否还无账号?点击注册</a></span>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>


import API from "@/api/request";

export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      form: {
        username: '',
        password: '',
        studentId: ''
      },
      loginRules: {
        studentId: [
          { required: true, message: '请输入学号', trigger: 'blur' },
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    Login(loginForm) {
      this.$refs[loginForm].validate((valid) => {
        if (valid) {
          //开启加载屏
          this.loading = true;
          API.post('/api/user/Login',this.form,{
            headers: {
              'Content-Type': 'application/json'
            }
          }).then(res => {
            console.log(res)
            if (res.data.code === 200) {
              this.$message({
                message: '登录成功',
                type: 'success',
                duration:1000
              });
              //保存用户信息
              sessionStorage.setItem("user", JSON.stringify(res.data.data))
              console.log('查看user'+sessionStorage.getItem('user'))              
              // 跳转主页
              this.$router.replace('/content');
            } else {
              this.$message({
                message: res.data.message,
                type: 'error'
              });
            }
          })
          //关闭加载屏
          this.loading = false;
        }
      });
    },
    // 注册
    register(){
      //清空当前用户信息
      sessionStorage.setItem("user",null);
      // 跳转注册页
      this.$router.replace('/register');
    }

  }
}
</script>

<style scoped>
.login-page {
  background-image: linear-gradient(180deg, #B0E0E6 0%, #20B2AA 100%);

  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-title {
  font-size: 20px;
}

.box-card {
  width: 375px;
}
</style>